using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Secretaria_NotasDiciembreMarzo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        cboCursos1.seleciona += new cboCurSelected(cboCursos1_seleciona);
        btnGuardar.Visible = false;
    }

    void cboCursos1_seleciona(string aselec)
    {
        FillGrid();
    }

    private void FillGrid()
    {
        
        if (cboCursos1.value != "")
        {
            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ColegiosConnectionString"].ToString());
            SqlCommand cmd = new SqlCommand();
            //string command = "select A.IdAlumno, A.Apellido + ', ' + A.Nombre as Nombre from Alumnos A inner join NotasFinales NF on (A.IdAlumno = NF.IdAlumno and IdMateria = " + cboCursos1.value + " and Final <6) order by A.Apellido";
            string command = "select A.IdAlumno, A.Apellido + ',' + A.Nombre as nombre, NDM.Diciembre from Alumnos A inner join NotasFinales NF on (A.IdAlumno = NF.IdAlumno and NF.IdMateria = " + cboCursos1.value + " and diciembre >0) left join NotasDiciembreMarzo NDM on (NDM.IdAlumno = A.IdAlumno and NDM.IdMateria = NF.IdMateria) order by A.Apellido";
            cmd.CommandText = command;
            cmd.CommandType = CommandType.Text;

            cmd.Connection = conn;

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet _DataSet = new DataSet();
            da.Fill(_DataSet);
            conn.Close();

            GridView1.DataSource = _DataSet;
            GridView1.DataBind();
        }

        if (GridView1.Rows.Count > 0)
            btnGuardar.Visible = true;

    }

    protected void btnGuardar_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ColegiosConnectionString"].ToString());
        SqlCommand cmd = new SqlCommand();

        cmd.CommandType = CommandType.Text;

        cmd.Connection = conn;

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            GridViewRow row = GridView1.Rows[i];
            string sNota = ((TextBox)row.FindControl("txtNotaDM")).Text;
            string IdAlumno = row.Cells[0].Text;
            string Materia = cboCursos1.value;

            if (sNota == "" || (int.Parse(sNota) >= 0 && int.Parse(sNota) <= 10))
            {
                cmd.CommandText = @"delete NotasDiciembreMarzo where IdAlumno = " + IdAlumno + " and IdMateria = " + Materia;
                SqlDataAdapter da1 = new SqlDataAdapter(cmd);
                DataSet _DataSet1 = new DataSet();
                da1.Fill(_DataSet1);

                if (sNota != "")
                {
                    cmd.CommandText = @"insert into NotasDiciembreMarzo (IdAlumno, IdMateria, Diciembre) values (" + IdAlumno + ", " + Materia + ", " + sNota + ")";
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataSet _DataSet = new DataSet();
                    da.Fill(_DataSet);
                }

            }
        }
        FillGrid();
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ColegiosConnectionString"].ToString());
            SqlCommand cmd = new SqlCommand();
            string command = "select * from NotasDiciembreMarzo where IdAlumno = " + e.Row.Cells[0].Text + " and IdMateria = " + cboCursos1.value;
            cmd.CommandText = command;
            cmd.CommandType = CommandType.Text;

            cmd.Connection = conn;

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet _DataSet = new DataSet();
            da.Fill(_DataSet);
            conn.Close();

            if (_DataSet.Tables[0].Rows.Count > 0 )
                ((TextBox)e.Row.FindControl("txtNotaDM")).Text = _DataSet.Tables[0].Rows[0]["Diciembre"].ToString();
        }
    }
}
